package com.shanling.shanlingcontroller;

import android.app.ActivityManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.multidex.MultiDex;
import com.chipsguide.lib.bluetooth.commands.BluetoothDeviceCommandProtocol;
import com.chipsguide.lib.bluetooth.extend.devices.BluetoothDeviceSoundBoxManager;
import com.chipsguide.lib.bluetooth.managers.BluetoothDeviceManager;
import com.google.gson.reflect.TypeToken;
import com.linkplay.lpvr.app.BaseCoreApplication;
import com.linkplay.lpvr.avslib.LPAVSManager;
import com.linkplay.lpvr.lpvrbean.LPDeviceConfiguration;
import com.linkplay.lpvr.utils.GsonCore;

import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.MybluetoothDevice;
import com.shanling.shanlingcontroller.music.player.PlaylistLoader;
import com.shanling.shanlingcontroller.services.BluetoothStateService;
import com.shanling.shanlingcontroller.services.csr.GAIABREDRProvider;
import com.shanling.shanlingcontroller.utils.ScanFileHelper;


import org.litepal.LitePal;


import java.util.List;

/**
 * Created by ch1 on 2019/9/25.
 */

public class CustomApplication extends BaseCoreApplication {
    private static CustomApplication instance;
    private BluetoothDeviceManager bluetoothDeviceManager;
    private  GAIABREDRProvider mGAIABREDRProvider;

    //记录蓝牙是否连接
    private boolean isConnected=false;
    public static CustomApplication getInstance (){
        return instance;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        instance=this;
        if(this.getPackageName().equals(getProcessName(this))){
            initBluetooth();
            initLitePal();
            createNotificationChannel();
            initMusic();
        }
    }
    private void initLitePal() {
        LitePal.initialize(this);
    }
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    private String getProcessName(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo proInfo : runningApps) {
            if (proInfo.pid == android.os.Process.myPid()) {
                if (proInfo.processName != null) {
                    return proInfo.processName;
                }
            }
        }
        return null;
    }

    private void createNotificationChannel() {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                int importance = NotificationManager.IMPORTANCE_LOW;
                NotificationChannel channel = new NotificationChannel("shanling", "ShanLing", importance);
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                if (notificationManager != null) {
                    notificationManager.createNotificationChannel(channel);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void initBluetooth() {
        bluetoothDeviceManager = BluetoothDeviceManager.getInstance(this)
                .setDeviceType(BluetoothDeviceCommandProtocol.DeviceType.LAMP_COLOR)
                .setConnectionType(BluetoothDeviceManager.ConnectionType.SPP_ONLY)
                .build();
        LPAVSManager lpavsManager = LPAVSManager.getInstance(this);
        lpavsManager.setLPDeviceConfiguration(GsonCore.fromModelJson(getString(R.string.device_list), new TypeToken<List<LPDeviceConfiguration>>() {
        }), getString(R.string.client_id));
        lpavsManager.setProtocolType(LPAVSManager.LpAvsProtocolType.LP_AVS_PROTOCOL_SPP);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(new Intent(this, BluetoothStateService.class));
        } else {
            startService(new Intent(this, BluetoothStateService.class));
        }
    }

    public void initMusic(){
        new Thread(() -> {
            ScanFileHelper.INSTANCE.scanFileAsync(CustomApplication.getInstance());
            PlaylistLoader.INSTANCE.createDefaultPlaylist(Constants.PLAYLIST_QUEUE_ID, getString(R.string.playlist_queue));
//            PlaylistLoader.INSTANCE.createDefaultPlaylist(Constants.PLAYLIST_HISTORY_ID, getString(R.string.item_history));
//            PlaylistLoader.INSTANCE.createDefaultPlaylist(Constants.PLAYLIST_LOVE_ID, getString(R.string.item_favorite));
        }).run();
    }
    public BluetoothDeviceManager getBluetoothDeviceManager() {
        return bluetoothDeviceManager;
    }

    public void setBluetoothDeviceManager(BluetoothDeviceManager bluetoothDeviceManager) {
        this.bluetoothDeviceManager = bluetoothDeviceManager;
    }

    public GAIABREDRProvider getmGAIABREDRProvider() {
        return mGAIABREDRProvider;
    }

    public void setmGAIABREDRProvider(GAIABREDRProvider mGAIABREDRProvider) {
        this.mGAIABREDRProvider = mGAIABREDRProvider;
    }

    public boolean isConnected() {
        return isConnected;
    }

    public void setConnected(boolean connected) {
        isConnected = connected;
    }
}
